<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('sheet_templates', function (Blueprint $table) {
            $table->id();
            $table->string('name')->comment('名称');
            $table->text('description')->comment('描述');
            $table->tinyInteger('type')->comment('类型 1:填报类 2:汇总类');
            $table->json('data')->comment('模板数据');
            $table->timestamps();
            $table->softDeletes();
            $table->comment('报表模板');
        });

        Schema::create('sheet_pivot', function (Blueprint $table) {
            $table->bigInteger('report_template_id')->comment('报表模板ID');
            $table->bigInteger('aggregate_template_id')->comment('报表模板ID');
            $table->string('frequency')->comment('频次');
            $table->comment('报表关系');
            $table->unique(['report_template_id', 'aggregate_template_id', 'frequency'], 'report_aggregate_frequency_unique');
        });

        Schema::create('task_templates', function (Blueprint $table) {
            $table->id();
            $table->string('name')->comment('名称');
            $table->string('frequency')->comment('频次');
            $table->bigInteger('sheet_template_id')->comment('报表模板ID');
            $table->string('description')->comment('描述');
            $table->json('attachment')->nullable()->comment('附件');
            $table->tinyInteger('status')->default(1)->comment('状态 1:启用 0:禁用');
            $table->timestamps();
            $table->softDeletes();
            $table->comment('任务模板');
        });

        Schema::create('tasks', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('sheet_template_id')->comment('报告模板ID');
            $table->bigInteger('task_template_id')->comment('任务模板ID');
            $table->string('frequency')->comment('频次');
            $table->string('name')->comment('名称');
            $table->mediumInteger('year')->comment('年份');
            $table->string('key')->comment('键');
            $table->date('start_date')->nullable()->comment('开始时间');
            $table->date('end_date')->nullable()->comment('结束时间');
            $table->tinyInteger('notify_type')->comment('提醒类型 1:短信');
            $table->tinyInteger('status')->comment('状态');
            $table->timestamps();
            $table->softDeletes();
            $table->comment('任务');
        });

        Schema::create('sub_tasks', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('task_id')->comment('任务ID');
            $table->bigInteger('district_id')->comment('区县ID');
            $table->json('data')->nullable()->comment('填报数据');
            $table->tinyInteger('status')->comment('状态');
            $table->dateTime('last_reminded_at')->nullable()->comment('上次提醒时间');
            $table->timestamps();
            $table->softDeletes();
            $table->index('district_id');
            $table->comment('区县任务');
        });

        Schema::create('sub_task_logs', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('sub_task_id')->comment('区县任务ID');
            $table->bigInteger('operator_id')->comment('操作人ID');
            $table->string('operation_type')->comment('操作类型');
            $table->text('comment')->nullable()->comment('备注');
            $table->integer('duration')->default(0)->comment('持续时间');
            $table->timestamps();
            $table->softDeletes();
            $table->index('sub_task_id');
            $table->index('operator_id');
            $table->rawIndex('(cast(`transferee_ids` as unsigned array))', 'transferee_ids_index');
            $table->comment('任务日志');
        });

        Schema::create('aggregate_reports', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('sheet_template_id')->comment('报表模板ID');
            $table->string('frequency')->comment('频次');
            $table->mediumInteger('year')->comment('年份');
            $table->string('key')->comment('键 月份(1-12)、季度(1-4)、半年度、年度等类型的值');
            $table->json('data')->comment('数据');
            $table->timestamps();
            $table->softDeletes();
            $table->comment('汇总表');
        });

        Schema::create('districts', function (Blueprint $table) {
            $table->id();
            $table->string('name')->comment('名称');
            $table->timestamps();
            $table->softDeletes();
            $table->comment('区县');
        });

        Schema::create('data_dicts', function (Blueprint $table) {
            $table->id();
            $table->tinyInteger('type')->default(0)->comment('1:市 2:区县');
            $table->bigInteger('district_id')->nullable()->comment('区县ID');
            $table->string('dict_code')->comment('字典值');
            $table->string('dict_desc')->default('')->comment('值说明');
            $table->string('category_code')->comment('分类编码');
            $table->string('category_desc')->comment('分类说明');
            $table->json('extra')->nullable()->comment('其他属性');
            $table->integer('order')->default(0)->comment('排序值');
            $table->tinyInteger('status')->default(1)->comment('状态 1:启用 0:禁用');
            $table->timestamps();
            $table->comment('数据字典');
        });

        Schema::table('users', function (Blueprint $table) {
            $table->bigInteger('district_id')->comment('区县ID');
            $table->string('department')->comment('部门');
            $table->string('phone')->comment('手机号');
            $table->unique('phone');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('sheet_templates');
        Schema::dropIfExists('task_templates');
        Schema::dropIfExists('tasks');
        Schema::dropIfExists('sub_tasks');
        Schema::dropIfExists('sub_task_logs');
        Schema::dropIfExists('aggregate_reports');
        Schema::dropIfExists('districts');
        Schema::dropIfExists('data_dicts');

        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('district_id');
            $table->dropColumn('department');
            $table->dropColumn('phone');
        });
    }
};
